﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;
using Autofac;
using Autofac.Integration.Mvc;
using LOMBARDISPICK.Model;
using LOMBARDISPICK.Domain.Services;
using System.Reflection;
using LOMBARDISPICK.Web.Helpers;

namespace LOMBARDISPICK.Web.Controllers
{
	public class AJAXController : BaseController
	{
		public AJAXController() { }

		#region AjaxLoadDropDown
		//[View]
		public ActionResult AjaxLoadDropDown(AJAX_LOAD_DROP_DOWN ALDD)
		{
			//using the Entity name passed in create an instance of that entities Service.
			string ServiceInterfaceClassName = "I" + ALDD.ENTITY_NAME + "Service";
			string ServiceClassName = ALDD.ENTITY_NAME + "Service";
			Type myInterfaceType = Type.GetType("LOMBARDISPICK.Domain.Services." + ServiceInterfaceClassName + ",LOMBARDISPICK.Domain");
			Type myType = Type.GetType("LOMBARDISPICK.Domain.Services." + ServiceClassName + ",LOMBARDISPICK.Domain");
			try
			{
				var ServiceInstance = DependencyResolver.Current.GetService(myInterfaceType);
				//find the method we are interested in
				//check the field value to determine if we can determine the parameter type.  This is done to avoid an AmbiguousMatchException
				MethodInfo method;
				method = myType.GetMethod(ALDD.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { }, null);
				if (method == null) { method = myType.GetMethod(ALDD.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { typeof(Int32) }, null); }
				if (method == null) { method = myType.GetMethod(ALDD.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { typeof(Int32), typeof(Int32) }, null); }
				if (method == null) { method = myType.GetMethod(ALDD.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { typeof(Int32), typeof(Int32), typeof(Int32) }, null); }
				if (method == null) { method = myType.GetMethod(ALDD.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)); }
				//if (ALDD.FIELD_VALUE[0] == 0 && ALDD.FIELD_VALUE.Length == 1)
				//{
				//    //attempt to find the method with no parameters.  
				//    method = myType.GetMethod(ALDD.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { }, null);
				//    //attempt to find the method with one parameter
				//    if (method == null)
				//    {
				//        method = myType.GetMethod(ALDD.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { typeof(Int32) }, null);
				//        //new Type[] { typeof(Int32), typeof(Int32) }
				//    }
				//}
				//else
				//{
				//    method = myType.GetMethod(ALDD.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance));
				//}
				//using reflection we have an instance of a class whose method will be invoked.
				IEnumerable<SelectListItem> JSONResult;
				//check the list length to determine if the method is expecting a list of ints or a single int.  Also check to see how many parameters are expected.
				if (ALDD.FIELD_VALUE.Length > 1)
				{
					JSONResult = (IEnumerable<SelectListItem>)method.Invoke(ServiceInstance, new object[] { ALDD.FIELD_VALUE.OfType<int>().ToList() });
				}
				else if (method.GetParameters().Count() == 0)
				{
					JSONResult = (IEnumerable<SelectListItem>)method.Invoke(ServiceInstance, null);
				}
				else
				{
					JSONResult = (IEnumerable<SelectListItem>)method.Invoke(ServiceInstance, new object[] { ALDD.FIELD_VALUE[0] });
				}
				//return the selectlist results to the view as a JSON object.
				return Json(JSONResult, JsonRequestBehavior.AllowGet);
			}
			//catch (System.Reflection.AmbiguousMatchException theException)
			catch (Exception e)
			{
				//HandleException.HandleExceptionAndLogResults(e);
				return null;
			}
		}

		//[View]
		public ActionResult AjaxGetJSONArray(AJAX_GET_JSON_ARRAY AGJA)
		{
			//using the Entity name passed in create an instance of that entities Service.
			string ServiceInterfaceClassName = "I" + AGJA.ENTITY_NAME + "Service";
			string ServiceClassName = AGJA.ENTITY_NAME + "Service";
			Type myInterfaceType = Type.GetType("LOMBARDISPICK.Domain.Services." + ServiceInterfaceClassName + ",LOMBARDISPICK.Domain");
			Type myType = Type.GetType("LOMBARDISPICK.Domain.Services." + ServiceClassName + ",LOMBARDISPICK.Domain");

			try
			{
				var ServiceInstance = DependencyResolver.Current.GetService(myInterfaceType);
				//find the method we are interested in
				//MethodInfo method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance));

				MethodInfo method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { }, null);
				if (method == null) { method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { typeof(Int32) }, null); }
				if (method == null) { method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { typeof(Int32), typeof(Int32) }, null); }
				if (method == null) { method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { typeof(Int32), typeof(Int32), typeof(Int32) }, null); }
				if (method == null) { method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)); }
				//using reflection we have an instance of a class whose method will be invoked.
				//IEnumerable<string[]> JSONResult;
				//check to see how many parameters are expected.
				if (AGJA.VALUES_PASSED_IN.Length > 1)
				{
					var JSONResult = method.Invoke(ServiceInstance, new object[] { AGJA.VALUES_PASSED_IN.OfType<int>().ToList() });
					return Json(JSONResult, JsonRequestBehavior.AllowGet);
				}
				if (method.GetParameters().Count() == 0)
				{
					//var JSONResult = (IEnumerable<string[]>)method.Invoke(ServiceInstance, null);
					var JSONResult = method.Invoke(ServiceInstance, null);
					return Json(JSONResult, JsonRequestBehavior.AllowGet);
				}
				else
				{
					var JSONResult = method.Invoke(ServiceInstance, new object[] { AGJA.VALUES_PASSED_IN[0] });
					return Json(JSONResult, JsonRequestBehavior.AllowGet);
				}
				//return the selectlist results to the view as a JSON object.
				//return Json(JSONResult, JsonRequestBehavior.AllowGet);
			}
			catch (Exception e)
			{
				//HandleException.HandleExceptionAndLogResults(e);
				return null;
			}
		}








		//[View]
		public ActionResult AjaxSetValue(AJAX_GET_JSON_ARRAY AGJA)
		{
			//using the Entity name passed in create an instance of that entities Service.
			string ServiceInterfaceClassName = "I" + AGJA.ENTITY_NAME + "Service";
			string ServiceClassName = AGJA.ENTITY_NAME + "Service";
			Type myInterfaceType = Type.GetType("LOMBARDISPICK.Domain.Services." + ServiceInterfaceClassName + ",LOMBARDISPICK.Domain");
			Type myType = Type.GetType("LOMBARDISPICK.Domain.Services." + ServiceClassName + ",LOMBARDISPICK.Domain");
			try
			{
				var ServiceInstance = DependencyResolver.Current.GetService(myInterfaceType);
				//find the method we are interested in
				//check the field value to determine if we can determine the parameter type.  This is done to avoid an AmbiguousMatchException
				MethodInfo method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { }, null);
				if (method == null) { method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { typeof(Int32) }, null); }
				if (method == null) { method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { typeof(Int32), typeof(Int32) }, null); }
				if (method == null) { method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance), null, CallingConventions.Any, new Type[] { typeof(Int32), typeof(Int32), typeof(Int32) }, null); }
				if (method == null) { method = myType.GetMethod(AGJA.METHOD_NAME, (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)); }

				//using reflection we have an instance of a class whose method will be invoked.
				//check to see how many parameters are expected.
				if (method.GetParameters().Count() == 0)
				{
					method.Invoke(ServiceInstance, null);
				}
				else
				{
					method.Invoke(ServiceInstance, new object[] { AGJA.VALUES_PASSED_IN[0] });
				}
				//return the selectlist results to the view as a JSON object.
				return Json("", JsonRequestBehavior.AllowGet);
			}
			//catch (System.Reflection.AmbiguousMatchException theException)
			catch (Exception e)
			{
				//HandleException.HandleExceptionAndLogResults(e);
				return null;
			}
		}

		#endregion
	}
}